(function($, doc) {
	$.plusReady(function() {
		var scan;
		mui.init({
			beforeback: function() {
				closeScan();
			}
		});

		var userinfo = app.getState();
		var uid = userinfo.user_id;

		// 扩展API加载完毕后调用onPlusReady回调函数 
		var styles = {
			frameColor: "#29E52C",
			scanbarColor: "#29E52C",
			background: ""
		};
		scan = new plus.barcode.Barcode('bcid', styles);
		//scan.close();
		scan.onmarked = onmarked;
		scan.start();

        // 二维码扫描成功
		function onmarked(type, result) {
			var text = '未知: ';
			switch(type) {
				case plus.barcode.QR:
					text = 'QR: ';
					break;
				case plus.barcode.EAN13:
					text = 'EAN13: ';
					break;
				case plus.barcode.EAN8:
					text = 'EAN8: ';
					break;
			}
			//alert(text + result);
			//scan.close();
			parseQrcode(result);
		}

		function parseQrcode(result) {
			var pre = result.substring(0, 2);//是否为钱包地址
			var str = result.replace(/\"/g, "").split('@');
			var type = str[0],
				rt = str[1];
			console.log(result);
			console.log('type:'+type);
			console.log('rt:'+rt);
			console.log('pre:'+pre);
			if((!type || !rt) && pre != "0x") {
				mui.alert('扫码失败');
				return;
			}
			var player = document.createElement('audio');
			player.src = '../sound/di.mp3';
			player.volume = 1;
			player.loop = false;
			player.play();
		    if(type == 'pay') {
				topay(rt);
			} else if(type == 'cash') {
				tocash(rt);
			} else {
				toaddress(result);
//				mui.alert('扫码失败');
//				return;
			}
		}

		
        //付款
		function topay(rt) {
			closeScan();
			var postUrl = apiurl + "Wallet/getWallets";
			var postdata = {
				from_id: rt,
				to_id: uid
			};

			$.post(postUrl, postdata, function(res) {
				if(res.status == 1) {
					var f = res.data.from;
					var t = res.data.to;
					var url = "wallet/sendTxEmb.html?fromtype=1&id=" + f['id'] + "&name=" + f['name'] + "&pubkey=" + f.pubkey + "&type=" + f.type + "&tokens=" + f.tokens + "&isdefault=1&user_id=" + uid + "&to=" + t.address;
					mui.openWindow({
						url: url,
						id: 'pages/chat/transfer_accounts',
						extras: {
							roomId: uid
						}
					});
				} else {
					mui.alert(res.msg);
				}
			});
		}

		
        //收款
		function tocash(rt) {
			var postUrl = apiurl + "User/getWallets";
			var postdata = {
				from_id: uid,
				to_id: rt,
				'_token':userinfo.token
			};

			$.post(postUrl, postdata, function(res) {
				if(res.status==-3){
					mui.toast(res.msg);
					app.out();
					return false;
				}
				
				if(res.status == 1) {
					var f = res.data.user;
					var t = res.data.tuser;
					  //打开下一步
						mui.openWindow({
							id:'roll_out.html',
							url:'/pages/order/roll_out.html',
							extras:{
						      other_uid: t.user_id,
						      other_phone: t.mobile,
						      other_headimgurl: t.headimgurl,
						      other_nickname: t.nickname,
						      type: 1
						    },
							show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
						});
				} else {
					mui.alert(res.msg);
				}
			});
		}

		function toaddress(result){
			closeScan();
			var targetWnd = plus.webview.currentWebview().opener();
			$.fire(targetWnd, 'scanaddress', {result:result});
			plus.webview.currentWebview().close();
		}

		function cancelScan() {
			scan.cancel();
		}

		function onPlusReady() {

			/*
			varself = plus.webview.currentWebview();
			varopener = self.opener();
			//scan.cancel();
			mui.fire(opener, 'endScan', {
				result: result,
				file: file
			});*/
		}

		function startRecognize() {
			scan = new plus.barcode.Barcode('bcid');
			scan.onmarked = onmarked;
		}

		function startScan() {
			scan.start();
		}

		function closeScan() {
			scan.cancel();
			scan.close();
		}
		// 从相册中选择二维码图片 
		function scanPicture() {
			plus.gallery.pick(function(path) {
				plus.barcode.scan(path, onmarked, function(error) {
					plus.nativeUI.alert("无法识别此图片");
				});
			}, function(err) {
				plus.nativeUI.alert("Failed: " + err.message);
			});
		}
		mui(".footer").on("tap", "#pic", function() {
			//scan.close();
			//scan.start();
			scanPicture();
		});
		$.back = function(event) {
			closeScan();
			plus.webview.currentWebview().close();
			//location.href = '../../find.html';
			//plus.webview.show('me.html', "slide-in-right", 300);
			return false;
		};
	});

}(mui, document));